package org.example.blog.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.blog.model.BlogInfo;
import org.example.blog.service.BlogService;
import org.example.blog.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        return blogService.getList();
    }

    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId, HttpServletRequest request){
        log.info("getBlogDetail 接收参数："+ blogId);
        if (blogId == null){
            throw new UnsupportedOperationException("blogId不能为空");
        }

        BlogInfo blogInfo = blogService.getBlogDetail(blogId);
//        博客作者id
        Integer authorId = blogInfo.getUserId();
//        获取当前登录用户id
        String userToken = request.getHeader("user_token_header");
        Integer userId = JwtUtils.GetUserIdFromToken(userToken);
//
        if (authorId == userId && authorId != null){
            blogInfo.setIsLoginUser(true);
        }
        return blogInfo;
    }

    /*
    *发布博客
    */
    @RequestMapping("/addBlog")
    public Boolean addBlog(@Validated @RequestBody BlogInfo blogInfo, HttpServletRequest request){
        log.info("addBlog 接收参数："+ blogInfo);
//        获取userID
        String userToken = request.getHeader("user_token_header");
//        从token中获取用户ID
        Integer UserId = JwtUtils.GetUserIdFromToken(userToken);
        blogInfo.setUserId(UserId);
        Integer result = null;
        try {
            result = blogService.insertBlog(blogInfo);
            if (result == 1){
                return true;
            }
        }catch (Exception e){
            log.error("发布博客失败，e：" + e.getMessage());
            e.printStackTrace();
        }
        return false;
    }

    /*
    * 更新博客
    * */
    @RequestMapping("/updateBlog")
    public boolean updateBlog(@Validated @RequestBody BlogInfo blogInfo){
        log.info("updateBlog接收参数，blogInfo："+blogInfo);
        Integer result = null;
        try {
            result = blogService.update(blogInfo);
            if (result <= 0){
                return false;
            }
        }catch (Exception e){
            log.error("updateBlog发生异常，e：{}",e.getMessage());
        }
        return true;
    }

    @RequestMapping("/deleteBlog")
    public boolean deleteBlog(Integer blogId){
        Assert.notNull(blogId,"blogId不能为空");
        log.info("deleteBlog接收参数："+blogId);
        Integer result = null;
        try {
            BlogInfo blogInfo = new BlogInfo();
            blogInfo.setId(blogId);
            blogInfo.setDeleteFlag(1);
            result = blogService.update(blogInfo);
            if (result <= 0){
                return false;
            }
        }catch (Exception e){
            log.error("deleteBlog 发生异常，e：" + e.getMessage());
        }
        return true;
    }
}
